Add full alt-text (title and description) support for pictures and inline shapes#1530
Open
EwoutH wants to merge 3 commits intopython-openxml:masterfrom
Open
Add full alt-text (title and description) support for pictures and inline shapes#1530EwoutH wants to merge 3 commits intopython-openxml:masterfrom
EwoutH wants to merge 3 commits intopython-openxml:masterfrom
Conversation
…ingProps Expose the `<wp:docPr>` attributes used for image title and alt text in Word. These correspond to the "Alt Text (Title)" and "Alt Text (Description)" fields shown in the Word UI. Based on the approach from PR python-openxml#227 ("Add support for image title and alt text"), limited here to adding OptionalAttribute definitions in the OXML layer.
- Extend StoryPart.new_pic_inline() with optional `title` and `descr` parameters. - Pass these through to CT_Inline.new_pic_inline() for setting <wp:docPr> attributes. - Update Run.add_picture() to accept and forward `title` and `descr` to StoryPart. Part of the effort to enable setting image alternative text (accessibility title/description) when adding pictures.
Expose read/write access to image alternative text stored in <wp:docPr> via new `InlineShape.alt_text` (description) and `InlineShape.alt_title` (title) properties.
Author
|
While adding alt-text, I ran into an API/compatibility issue. I’d like to let users set alt text at insertion time via:
These are public APIs. Internally they call:
However, the existing API and tests assume a 3-arg positional call chain: document.add_picture(path, width, height)
run.add_picture(path, width, height)
part.new_pic_inline(image, width, height)If we extend this with positional
I think there are three options:
@scanny Which option would you prefer? |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Builds on #227 and #317.
Summary
This PR introduces first-class support for Word Alt Text in
python-docx. Users can now set and read both the Alt Text Title and Alt Text Description (as shown in Word’s Accessibility pane) directly through the API when inserting or editing pictures.Motive
Previously,
python-docxdid not expose a clean API for working with image alternative text. Users had to manually manipulate XML to set or retrieve<wp:docPr>attributes (title,descr). This enhancement improves accessibility workflows and enables automated tools (e.g., alt-text generators) to embed compliant descriptions directly into Word documents.Implementation
CT_NonVisualDrawingProps(oxml/shape.py): Added optional attributestitleanddescr.CT_Inline.new()/new_pic_inline(): Accept and applytitle/descrto<wp:docPr>.StoryPart.new_pic_inline(): Addedtitleanddescrparameters and forwarded them toCT_Inline.new_pic_inline().Run.add_picture()andDocument.add_picture(): Updated to accept and pass throughtitleanddescrfor inline pictures.InlineShape(shape.py): Added newalt_textandalt_titleproperties for convenient read/write access todocPr.descranddocPr.title.Usage Examples
Additional Notes
alt_titleand alt_text persisted correctly.titleanddescrare optional.